from config import CONFIG

import subprocess
import logging
import os
import time

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)

services = []

nginx_dir = CONFIG["nginx"]['path']
os.chdir(nginx_dir)
services.append(
    {"name": "nginx", "process":subprocess.Popen(['nginx', '-c', 'conf/nginx.conf'])}
)

os.chdir('..')
for deploy in CONFIG["deploy"]:
    services.append(
        {"name":"daphne", "process":subprocess.Popen(['daphne', 'mysite.asgi:application', '-p', f"{deploy['port']}", '-b', deploy["host"]])}
    )


services.append(
    {"name":"scanner","process":subprocess.Popen(['python', 'fs_scanner.py'])}
)

for process_entry in services:
    logging.info(f'{process_entry["name"]} {process_entry["process"].pid}')

while True:
    time.sleep(2)